package com.yequan.drawerlayoutdemo;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/2/15 0015.
 */

public class CompanyInfoAdapter extends RecyclerView.Adapter<CompanyInfoAdapter.BindingViewHolder> {

    private static final String TAG = "CompanyInfoAdapter";
    private static final int TYPE_ITEM_APPROVED_TRUE = 1;
    private static final int TYPE_ITEM_APPROVED_FALSE = 2;
    private Context mContext;
    private List<CompanyInfoEntry> companyInfoEntryList;
    private LayoutInflater inflater;
    private OnCompanyItemClickListener mListener;

    public CompanyInfoAdapter(Context context) {
        this.mContext = context;
        inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        companyInfoEntryList = new ArrayList<>();
    }

    @Override
    public BindingViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ViewDataBinding binding = null;
        switch (viewType) {
            case TYPE_ITEM_APPROVED_TRUE:
                binding = DataBindingUtil.inflate(inflater, R.layout.item_approved, parent, false);
                break;
            case TYPE_ITEM_APPROVED_FALSE:
                binding = DataBindingUtil.inflate(inflater, R.layout.item_unapproved, parent, false);
                break;
        }
        return new BindingViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(BindingViewHolder holder, int position) {
        final CompanyInfoEntry companyInfoEntry = companyInfoEntryList.get(position);
        holder.getBinding().setVariable(com.yequan.drawerlayoutdemo.BR.companyInfoItem,
                companyInfoEntry);
        holder.getBinding().executePendingBindings();
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onCompanyItemClick(companyInfoEntry);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return companyInfoEntryList.size();
    }

    @Override
    public int getItemViewType(int position) {
        boolean isMarket = companyInfoEntryList.get(position).isMarket();
        if (isMarket) {
            return TYPE_ITEM_APPROVED_TRUE;
        } else {
            return TYPE_ITEM_APPROVED_FALSE;
        }
    }

    interface OnCompanyItemClickListener {
        void onCompanyItemClick(CompanyInfoEntry companyInfoEntry);
    }

    public void setOnCompanyItemListener(OnCompanyItemClickListener listener) {
        this.mListener = listener;
    }

    public void addAll(List<CompanyInfoEntry> list) {
        companyInfoEntryList.addAll(list);
        notifyDataSetChanged();
    }

    public void add(CompanyInfoEntry companyInfoEntry) {
        companyInfoEntryList.add(companyInfoEntry);
        notifyDataSetChanged();
    }

    public void add(int position, CompanyInfoEntry companyInfoEntry) {
        companyInfoEntryList.add(position, companyInfoEntry);
        notifyItemInserted(position);
    }

    public void remove(CompanyInfoEntry companyInfoEntry) {
        if (companyInfoEntryList.size() == 0) {
            return;
        }
        companyInfoEntryList.remove(companyInfoEntry);
        notifyDataSetChanged();
    }

    public void remove(int position) {
        if (companyInfoEntryList.size() == 0) {
            return;
        }
        notifyItemRemoved(position);
    }

    class BindingViewHolder<T extends ViewDataBinding> extends RecyclerView.ViewHolder {

        private T mBinding;

        public BindingViewHolder(T binding) {
            super(binding.getRoot());
            this.mBinding = binding;
        }

        public T getBinding() {
            return mBinding;
        }

    }
}
